/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coordinates;

import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;

/**
 *
 * @author Bhagya
 */
public class ConvexHull {

    private CvMemStorage storage = CvMemStorage.create();
    private CvSeq hull;
    private int result1, result2;
    CvPoint leastPoint = null;

    public CvPoint getFingerTip(CvSeq largestcontour) {

        if (largestcontour != null && !(largestcontour.isNull())) {

            hull = cvConvexHull2(largestcontour, storage, CV_COUNTER_CLOCKWISE, 1);
            result2 = 0;

            for (int i = 0; i < hull.total(); i++) {
                CvPoint hullPoints = new CvPoint(cvGetSeqElem(hull, i));
                result1 = hullPoints.y();
                if (result1 > result2) {
                    result2 = result1;
                    leastPoint = hullPoints;
                }
            }
            cvClearMemStorage(storage);
            cvClearSeq(hull);
        }
        return leastPoint;
    }
}
